package org.apache.pig.pigpen.util;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.pig.PigServer;
import org.apache.pig.impl.util.PropertiesUtil;
import org.apache.pig.pigpen.ConsoleLogger;
import org.apache.pig.pigpen.LoggerUtil;
import org.apache.pig.pigpen.PigPenActivator;
import org.apache.pig.pigpen.PluginConfiguration;
import org.apache.pig.pigpen.preferences.PreferenceConstants;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Text;

public class PigLauncher_2 implements Runnable{

	ConsoleLogger logger = ConsoleLogger.getDefault();
	
    static final boolean forceLocalExec = true; // helps with debugging

    IFile file;
    Text output;

    Process proc = null;

    public PigLauncher_2(IFile file, Text output) {
        this.file = file;
        this.output = output;

        logger.clear();
        logger.setFocus();
    }

    
	@Override
	public void run() {
        try {
            // run Pig in separate process (don't want to run it in the UI
            // thread!)

            logger.error("Launching the job!");
            List<String> args = new LinkedList<String>();

            File jvm = new File(
                    new File(System.getProperty("java.home"), "bin"), "java");
            args.add(jvm.toString());


//            args.add("-classpath");
//            IPreferenceStore store = PigPenActivator.getDefault()
//                    .getPreferenceStore();
//            String stringList = store
//                    .getString(PreferenceConstants.P_PROPERTIES);
//            Map<String, String> PrefProperties = PluginConfiguration
//                    .parseString(stringList);
//            String confPath = PrefProperties
//                    .get(PreferenceConstants.P_CONFPATH);
//            if (confPath == null || confPath.length() == 0) {
//
//                logger
//                        .debug("Please set the variable "
//                                + PreferenceConstants.P_CONFPATH
//                                + " to point to the directory containing the configuration files in Pig preferences");
//                return;
//            }
//            logger.info("Using the configuration from " + confPath);
//
//            args.add(classPath.toString() + sep + confPath);

            // String sshGateway =
            // PrefProperties.get(PreferenceConstants.P_SSHGATEWAY);
//            Properties properties = PropertiesUtil.loadPropertiesFromFile();
//            String sshGateway = properties.getProperty("ssh.gateway");
//
//            if (sshGateway != null && !"".equals(sshGateway) /*
//                                                              * sshGateway.length
//                                                              * () > 0
//                                                              */) {
//                logger.info("Using ssh.gateway = " + sshGateway);
//                args.add("-Dssh.gateway=" + sshGateway);
//            }

            // args.add(classPath.toString() + ":" +
            // "/home/shubhamc/workspace/eclipsePlugin/conf");
            args.add("org.apache.pig.pigpen.util.PigExample");

//            args.add("-exectype");
//            args.add("mapreduce");

            String filename = file.getRawLocation().toPortableString();// +
            // "tmp."
            // +
            // file.
            // getRawLocation
            // ().
            // getFileExtension
            // ();
            // String filename = file.getPath();

            args.add(filename);
            
            PigServer pig=PigServerPool.getPigServer();
            pig.registerScript(filename);
//            ProcessBuilder pb = new ProcessBuilder(args);
//            String parentDir = file.getRawLocation().removeLastSegments(1)
//                    .toPortableString();
//            // String parentDir = file.getParent();
//            pb.directory(new File(parentDir)); // set process working directory
//            // to directory that the .pig
//            // file is in
//            proc = pb.start();
//
//            // redirect stdout and stderr to us
//            StreamGobbler errorGobbler = new StreamGobbler(proc
//                    .getErrorStream(), this.output, true);
//            StreamGobbler outputGobbler = new StreamGobbler(proc
//                    .getInputStream(), this.output, false);
//
//            // kick off the stream gobblers in separate threads
//            /*
//             * output.getDisplay().asyncExec(errorGobbler);
//             * output.getDisplay().asyncExec(outputGobbler);
//             */
//            Thread errorT = new Thread(errorGobbler);
//            Thread outputT = new Thread(outputGobbler);
//            errorT.start();
//            outputT.start();

        } catch (Throwable t) {
            ConsoleLogger.getDefault().error("Error launching Pig"+ t);
        }
		
	}
}
